package com.dai.spec.gxk.service.impl;

import com.dai.spec.common.constant.RoleConstants;
import com.dai.spec.common.core.domain.entity.SysUser;
import com.dai.spec.common.exception.base.BaseException;
import com.dai.spec.common.utils.SecurityUtils;
import com.dai.spec.common.utils.StringUtils;
import com.dai.spec.gxk.mapper.MyScoreMapper;
import com.dai.spec.gxk.mapper.StudentMapper;
import com.dai.spec.gxk.request.MyScorePageRequest;
import com.dai.spec.gxk.service.MyScoreService;
import com.dai.spec.gxk.vo.MyScoreVO;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyScoreServiceImpl implements MyScoreService {
    private StudentMapper studentMapper;
    private MyScoreMapper mapper;

    public MyScoreServiceImpl(StudentMapper studentMapper, MyScoreMapper mapper) {
        this.studentMapper = studentMapper;
        this.mapper = mapper;
    }

    /**
     *查询我的成绩列表
     * @param pageRequest
     * @return
     */
    @Override
    public List<MyScoreVO> queryMyScoreList(MyScorePageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getPageSize());
        SysUser user = SecurityUtils.getLoginUser().getUser();

        Long roleId = user.getRoles().get(0).getRoleId();
        if (!RoleConstants.STUDENT.equals(roleId)) {
            throw new BaseException("当前登录用户不为学生，无权限！");
        }
        String studentNumber = user.getUserName();
        if (StringUtils.isNotBlank(studentNumber)){
            pageRequest.setStudentNumber(studentNumber);
            List<MyScoreVO> myScoreVOS = mapper.queryMyScoreList(pageRequest);
            return myScoreVOS;
        }
        return null;
    }
}
